package org.nulldevice.tankshot.images;

import java.awt.Graphics;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;

public class ImageRepresentant {
	/** The image to be drawn for this imagerepresentant */
	private BufferedImage image;
	protected int offsetX;
	protected int offsetY;

	/**
	 * Create a new imagerepresentant based on an image
	 * 
	 * @param image
	 *            The image that is this imagerepresentant
	 */
	public ImageRepresentant(BufferedImage image, int offsetX, int offsetY) {
		this.image = image;
		this.offsetX = offsetX;
		this.offsetY = offsetY;
	}

	/**
	 * Get the width of the drawn imagerepresentant
	 * 
	 * @return The width in pixels of this imagerepresentant
	 */
	public int getWidth() {
		return image.getWidth(null);
	}

	/**
	 * Get the height of the drawn imagerepresentant
	 * 
	 * @return The height in pixels of this imagerepresentant
	 */
	public int getHeight() {
		return image.getHeight(null);
	}

	/**
	 * Draw the imagerepresentant onto the graphics context provided
	 * 
	 * @param g
	 *            The graphics context on which to draw the imagerepresentant
	 * @param x
	 *            The x location at which to draw the imagerepresentant
	 * @param y
	 *            The y location at which to draw the imagerepresentant
	 */
	public void draw(Graphics g, int x, int y, double angle) {
		// TODO Optimize here, so that no new created objects are needed
		// everytime
		AffineTransform rotation;
		AffineTransformOp rotator;
		rotation = new AffineTransform();
		rotation.rotate(Math.toRadians(angle), offsetX, offsetY);
		rotator = new AffineTransformOp(rotation,
				AffineTransformOp.TYPE_BICUBIC);
		g
				.drawImage(rotator.filter(image, null), x - offsetX, y
						- offsetY, null);

		// g.drawImage(image,x,y,null);
	}
}